<?php
namespace app\common\model\coll;

use app\api\controller\Pub;
use app\common\model\company\Company;
use app\common\model\user\User;
use think\Model;

class Collection extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'collection';
    protected $pk    = 'CollectionId';

    const STATESTOP   = 1;
    const STATEINT    = 2;
    const STATEOK     = 2;
    const STATEDELETE = 3;

    const SELFCOMPANYID = -1; //本公司id

    public function userinfo()
    {
        return $this->hasOne('app\common\model\user\User', 'UserId', 'UserId');
    }

    public function companyinfo()
    {
        return $this->hasOne('app\common\model\company\Company', 'CompanyId', 'CompanyId');
    }

    public static function getStatusText($value = '')
    {
        $status = [Collection::STATESTOP => '未启用', Collection::STATEOK => '已启用', Collection::STATEDELETE => '已删除', '101' => '账号正常', '102' => '账号已锁定', '103' => '账号已删除'];
        if (empty($value)) {
            return $status;
        } else {
            return $status[$value];
        }
    }

    /**
     * 创建财务信息信息
     * 若有公司id则为公司账号
     * 没有的话就是个人账号
     * @param  [type]  $data   [财务数据]
     * @param  boolean $isselfcompany [是否为公司账号]
     * @return [type]          [description]
     */
    public static function user_create($data, $isselfcompany = false)
    {
        if ($isselfcompany == false) {
            //判断是否为产废单位角色
            $userinfo = User::getinfo();
            if ($userinfo->Role < 200) {
                //小于200的角色值为产废单位角色
                $companyinfo = Company::user_getinfo();
                if ($companyinfo == false) {
                    return moerr('请先完善企业信息', '', 'gohawacreate');
                }
            }
            $userlogininfo = Pub::getuserid();
            $userid        = $userlogininfo['msg'];
            $Collection    = new Collection;
            //设置企业id
            if (!empty($companyinfo)) {
                $Collection->CompanyId = $companyinfo->CompanyId;
            }
            //系统参数用户的设置后默认为启用
            $Collection->State  = Collection::STATEINT;
            $Collection->UserId = $userid;
        } else {
            $Collection    = new Collection;
            $Collection->State     = Collection::STATESTOP;
            $Collection->CompanyId = Collection::SELFCOMPANYID;
        }

        $Collection->UserName   = $data['UserName'];
        $Collection->CardNumber = $data['CardNumber'];
        $Collection->CardName   = $data['CardName'];
        $Collection->UserTel    = $data['UserTel'];

        $Collection->UpTime = date('Y-m-d H:i', time());
        $Collection->save();
        return $Collection;
    }

    public static function usergetlist($data, $is_page = true)
    {

        $where = [];
        if (!empty($data['CompanyId'])) {
            $where[] = ['CompanyId', '=', $data['CompanyId']];
        }
        if (!empty($data['UserId'])) {
            $where[] = ['UserId', '=', $data['UserId']];
        }
        if (!empty($data['where']['StartTime']) && $data['where']['StartTime'] != '') {
            $where[] = ['UpTime', '>=', $data['where']['StartTime']];
        }
        if (!empty($data['where']['EndTime']) && $data['where']['EndTime'] != '') {
            $where[] = ['UpTime', '<=', $data['where']['EndTime']];
        }
        $rdata = Collection::where($where);

        $visible = ['State', 'CardName', 'CardNumber', 'UserName', 'UserTel', 'UpTime', 'CompanyId', 'UserId','CollectionId'];
        if (!empty($data['field'])) {
            $visible = array_merge($data['field'], $visible);
        }
        $option = come_model_option($data, $rdata, $is_page);

        $rdata = $option['rdata'];
        $data  = $option['data'];

        if (!empty($data['field'])) {
            $rdata = $rdata->field($visible);
        }
        $rdata = $rdata->select();
        // $status_arr = [1 => '已启用', 2 => '未启用'];
        // foreach ($rdata as &$v) {
        //     $v['CompanyName'] = $v->companyinfo->Name ?? '';
        //     $v['UserName'] = $v->userinfo->Name ?? '';
        //     $v['StatusText'] = $status_arr[$v['State']];
        // }
        // unset($v);

        if (empty($data['field'])) {
            $rdata = $rdata->visible($visible);
        }
        $ndata['statetext'] = self::getStatusText();
        $ndata['list']      = $rdata->toArray();
        // $goodid = get_array_id($ndata,'GoodId','list','listinfo');
        if ($is_page) {
            $ndata['pageinfo'] = ['pageindex' => $data['pageindex'], 'pagemax' => $data['count']];
        }
        return $ndata;
    }
    /**
     * 获取信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function user_getinfo($id = '', $hidden = [])
    {
        $userlogininfo = Pub::getuserid();
        $userid        = $userlogininfo['msg'];
        if (!empty($id)) {
            $data = Collection::where('CollectionId', $id)->find();
        } else {
            $data = Collection::where('UserId', $userid)->where('State', '<>', Collection::STATEDELETE)->find();
        }
        if (empty($data)) {
            return false;
        }
        if (!empty($hidden)) {
            $data = $data->hidden($hidden);
        }
        return $data;
    }
    /**
     * 获取企业结算信息
     * @param  [type] $id     [description]
     * @param  [type] $hidden [description]
     * @return [type]         [description]
     */
    public static function get_company_info($id, $hidden = [])
    {
        $data = Collection::where('CompanyId', $id)->where('State', '<>', Collection::STATEDELETE)->find();
        if (empty($data)) {
            return [];
        }
        $data->hidden($hidden);
        return $data;
    }

    /**
     * 用户修改企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_reinfo($data)
    {
        $odata    = self::user_getinfo();
        $userinfo = User::getinfo();
        $file     = ['UpTime', 'CardName', 'CardNumber', 'UserName', 'UserTel'];
        $nreinfo  = 0;
        foreach ($data as $key => $value) {
            $sb = $key;
            if (!in_array($sb, $file)) {
                continue;
            }
            $odata->$sb = $value;
            $nreinfo++;
        }
        if (!empty($nreinfo)) {
            // $odata->State = Collection::STATEINT;
            $odata->save();
        }
    }

    /**
     * 管理列表
     * @return [type] [description]
     */
    public static function mgergetlist($data, $state = -1)
    {
        if ($state == -1) {
            $rdata = Collection::where('State', '<>', Collection::STATEDELETE);
        } else {
            $rdata = Collection::where('State', $state);
        }
        if (!empty($data['where'])) {
            $rdata = $data->where($data['where']);
        }

        $rdata = $rdata->with('userinfo');
        $rdata = $rdata->select();
        $rdata->visible(['UserId', 'UpTime', 'State', 'UserName', 'CardNumber', 'CarNumber', 'UserTel', 'UserImg', 'CarImg', 'SettingTime', 'SettingUserId', 'Address', 'userinfo.Name', 'userinfo.Iphone', 'userinfo.State']);
        $ndata['list']      = $rdata->toArray();
        $ndata['statetext'] = Collection::getStatusText();
        return $ndata;
    }
}
